package cn.smartorigin.soas.frame;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import cn.smartorigin.soas.data.SqliteConnecter;
import cn.smartorigin.soas.data.TestData;
import cn.smartorigin.soas.frame.panel.ManagePlateTypePage;

public class AddPlateTypeFrame extends JFrame{
	
	//MainFrame mf = null;
	JButton mCfnBtn, mClrBtn;
	JTextField mPlateName;
	ManagePlateTypePage mtp;

	public AddPlateTypeFrame(ManagePlateTypePage mt){
		//mf = mainframe;
		this.mtp = mt;
		this.setTitle("添加镀种  - SOAS");
		this.setIconImage(new ImageIcon("img/sosa_logo.png").getImage());
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setSize(600,400);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		initBackGround();
		this.init();
		this.setVisible(true);
	}
	
	private void init(){
		fontInit();
		compentInit();
	}
	
	private void initBackGround(){	  
		  ImagePanel ip = new ImagePanel(new ImageIcon("res/images/dialog_bg.png"));
		  this.setContentPane(ip); 
	}
	
	private void fontInit(){
		Font font = new Font("宋体", Font.PLAIN, 12);
		UIManager.put("Button.font", font);
		UIManager.put("Label.font", font);

	}
	
	private void compentInit(){
		
		BtnListener btnlistener = new BtnListener();
		
		ImageIcon clrBtnIcon=new ImageIcon("img/ic_delete.png"); 
		ImageIcon clrCfnIcon=new ImageIcon("img/corfim.png");
		mCfnBtn = new JButton("确定", clrCfnIcon);
		mCfnBtn.setBounds(250,300,100,30);
		mCfnBtn.addActionListener(btnlistener);
		mClrBtn = new JButton("取消",clrBtnIcon);
		mClrBtn.setBounds(450,300,100,30);
		mClrBtn.addActionListener(btnlistener);
		
		JLabel clientName  = new JLabel("镀种名称：");
		clientName.setFont(new Font(clientName.getFont().getName(),Font.BOLD,clientName.getFont().getSize()));
		clientName.setBounds(250,150,100,30);
//		JLabel clientDesc  = new JLabel("客户简介：");
//		clientDesc.setFont(new Font(clientDesc.getFont().getName(),Font.BOLD,clientDesc.getFont().getSize()));
//		clientDesc.setBounds(150,150,100,30);
		mPlateName = new JTextField();
		mPlateName.setBounds(350,150,200,30);
//		mClientDesc = new JTextField();
//		mClientDesc.setBounds(250,150,200,30);
		
		this.setLayout(null);
		this.add(clientName);
//		this.add(clientDesc);
		this.add(mPlateName);
//		this.add(mClientDesc);
		this.add(mCfnBtn);
		this.add(mClrBtn);
	}
	
	class BtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			Object  obj =(Object)e.getSource();
			if(obj == mCfnBtn){
				saveData();
//				mf.reflashData();
				mtp.flashData();
				frameClose();
			}else if(obj == mClrBtn){
				frameClose();
			}
		}
	}
	
	void saveData(){
		String str1 = mPlateName.getText();
		if((str1!=null))
			try {
				TestData.insertPlateType(new SqliteConnecter(), str1);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	private  void frameClose(){
		this.dispose();
	}
}
